/*************************************************************************
	> File Name: State.cc
	> Author: Joker
	> Mail: mahongtao@foxmail.com 
	> Created Time: 2020-09-22 18:20:15
 ************************************************************************/

#include "State.h"

#include <iostream>

using std::cout;
using std::endl;

Context::Context(State * pState)
    :_pState(pState)
{
    cout << "Context(State *)" << endl;
}

Context::~Context(){
    cout << "~Context()" << endl;

    delete _pState;
    _pState = nullptr;
}

void Context::Request(){
    if(nullptr != _pState){
        _pState->Handle(this);
    }
}

void Context::ChangeState(State * pState){
    if(nullptr != _pState){
        delete _pState;
        _pState = nullptr;
    }

    _pState = pState;
}

void ConcreateStateA::Handle(Context * pContext){
    cout << "Handle by ConcreateStateA" << endl;

    if(nullptr != pContext){
        pContext->ChangeState(new ConcreateStateB());
    }
}

void ConcreateStateB::Handle(Context * pContext){
    cout << "Handle by ConcreateStateB" << endl;

    if(nullptr != pContext){
        pContext->ChangeState(new ConcreateStateA());
    }
}
